package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.model.Board;
import com.conversationboard.model.Boards;
import com.conversationboard.model.User;

@WebServlet(name = "AdminPreRemoveUsersControllerServlet", urlPatterns = "/Admin/AdminPreRemoveUsersControllerServlet")
public class AdminPreRemoveUsersControllerServlet extends HttpServlet {

	private static final long serialVersionUID = 4677392194110767210L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		try {

			User user = User.get(request.getUserPrincipal().getName());
			int boardId = Integer.parseInt(request.getParameter("boardid"));
			Board board = Boards.getBoard(boardId);

			if (!user.isBoardAdmin(boardId)) {
				return;
			}

			List<String> members = board.getBoardMembers();

			request.setAttribute("members", members);
			request.setAttribute("board", board);
			request.setAttribute("user", user);

			RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/Admin/remove-user-from-closed-board.jsp");
			dispatcher.forward(request, response);

		} catch (NumberFormatException e) {
		} catch (SQLException e) {
			throw new ServletException(e);
		}

	}

}
